package org.lex.dbq.model.service;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.lex.dbq.model.bo.BaseObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class BaseService<T extends BaseObject> {
	protected Logger log = LoggerFactory.getLogger(getClass());
	protected Map<String, T> idMap = new ConcurrentHashMap<String, T>();

	public int size() {
		return idMap.size();
	}

	public void add(T o) {
		idMap.put(o.getId(), o);
	}

	public T getById(String id) {
		return idMap.get(id);
	}

	public Collection<T> getAllObjects() {
		return idMap.values();
	}

	public Collection<String> getAllIds() {
		return idMap.keySet();
	}

	public T remove(T o) {
		return null == o ? null : idMap.remove(o.getId());
	}

	public T removeById(String id) {
		return idMap.remove(id);
	}

	public abstract void store() throws IOException;
}
